/**分享策略及相关字段 */
var GShare = {
    /**分享类型：签到双倍 场景id：1*/
    signDouble: "signDouble",
    /**分享类型：签到补签 场景id：2*/
    supplement: "supplement",
    /**分享类型：离线翻倍 场景id：3*/
    offLine: "offLine",
    /**分享类型：结算翻倍 场景id：4*/
    account: "account",
    /**分享类型：黄金武器 场景id：5*/
    goldWeapon: "goldWeapon",
    /**分享类型：挂机使用 场景id：6*/
    hangUp: "hangUp",
    /**分享类型：更多金币 场景id：7*/
    moreGold: "moreGold",
    /**分享类型：更多钻石 场景id：8*/
    moreDiamond: "moreDiamond",
    /**分享类型：掉落钻石 场景id：9*/
    dropDiamond: "dropDiamond",
    /**分享类型：获取技能 场景id：12*/
    getSkill: "getSkill",
    /**分享类型：签到奖励 场景id：13*/
    signAward: "signAward",
    /**分享类型：高级宠物 场景id：14*/
    seniorPet: "seniorPet",
    
    /**获取分享场景对应的index */
    getIndexByShareType(shareType){
        if(shareType == undefined){
            return -1;
        }
        var index = 0;
        if(shareType.indexOf(GShare.signDouble) > -1){
            index = 0;
        }
        if(shareType.indexOf(GShare.supplement) > -1){
            index = 1;
        }
        if(shareType.indexOf(GShare.offLine) > -1){
            index = 2;
        }
        if(shareType.indexOf(GShare.account) > -1){
            index = 3;
        }
        if(shareType.indexOf(GShare.goldWeapon) > -1){
            index = 4;
        }
        if(shareType.indexOf(GShare.hangUp) > -1){
            index = 5;
        }
        if(shareType.indexOf(GShare.moreGold) > -1){
            index = 6;
        }
        if(shareType.indexOf(GShare.moreDiamond) > -1){
            index = 7;
        }
        if(shareType.indexOf(GShare.dropDiamond) > -1){
            index = 8;
        }
        if(shareType.indexOf(GShare.getSkill) > -1){
            index = 11;
        }
        if(shareType.indexOf(GShare.signAward) > -1){
            index = 12;
        }
        if(shareType.indexOf(GShare.seniorPet) > -1){
            index = 13;
        }
        return index;
    },

    /**根据不同的分享类型为其分配对应的视频广告位 */
    getAdUnit(shareType){
        if(shareType.indexOf(GShare.signDouble) > -1){
            return "adunit-dd49184adf16d321";
        }
        if(shareType.indexOf(GShare.supplement) > -1){
            return "adunit-b9bc4c91bc8115f1";
        }
        if(shareType.indexOf(GShare.offLine) > -1){
            return "adunit-1222c3f47cb84f26";
        }
        if(shareType.indexOf(GShare.account) > -1){
            return "adunit-0f4f92a7d7dcfa1c";
        }
        if(shareType.indexOf(GShare.goldWeapon) > -1){
            return "adunit-84268e85b8523311";
        }
        if(shareType.indexOf(GShare.hangUp) > -1){
            return "adunit-6fc0157ce9b3cad5";
        }
        if(shareType.indexOf(GShare.moreGold) > -1){
            return "adunit-9b4cbbdab75111fa";
        }
        if(shareType.indexOf(GShare.moreDiamond) > -1){
            return "adunit-56e62831e5f1aaea";
        }
        if(shareType.indexOf(GShare.dropDiamond) > -1){
            return "adunit-128ec4b74f087fb8";
        }
        if(shareType.indexOf(GShare.getSkill) > -1){
            return "adunit-53f0c102a62f135f";
        }
        if(shareType.indexOf(GShare.signAward) > -1){
            return "adunit-d5c0193bf26782ce";
        }
        if(shareType.indexOf(GShare.seniorPet) > -1){
            return "adunit-a78da4975656afe7";
        }
    },

    /**=>分享接口：分享视频逻辑中的分享接口，同时也是普通纯分享的接口  
     * shareType: 分享类型，可选，默认为当前分享场景
     */
    share(shareType){
        if(cc.sys.platform == cc.sys.WECHAT_GAME){
            if(!shareType){
                //标记进入分享逻辑
                GShare.isShare = true;
                GShare.isNormalShare = false;
                shareType = GShare.shareType;
            }else{
                GShare.isShare = false;
                GShare.isNormalShare = true;
            }
            //获取场景值
            var index = GShare.getIndexByShareType(shareType) + 1;
            //获取分享信息
            var info = CSharePngAndTittle.getTiByShareType(shareType);
            wx.updateShareMenu({
                withShareTicket: true,//分享到群
            });
            GShare.cancel = false;
            wx.shareAppMessage({
                //分享文案和图
                title: info.title,
                imageUrl: "https://fcs-1256906012.cos.ap-guangzhou.myqcloud.com/Line/png/" + info.id + ".png",
                query: "&cover=" + info.id + "&scene=" + index,
                cancel:res=>{
                    if(shareType){
                        GShare.cancel = true;
                    }
                }
            });
            //发送分享上报
            GReport.sendClick(1, info.id, index);
            GReport.scene(2, index);
        }
    },
    /**视频接口：分享视频逻辑中的分享接口 */
    videoAd(){
        if(cc.sys.platform == cc.sys.WECHAT_GAME){
            //获取场景值
            var index = GShare.getIndexByShareType(GShare.shareType) + 1;
            GReport.scene(5, index);
            var AdUnit = this.getAdUnit(GShare.shareType);
            var rewardedVideoAd = wx.createRewardedVideoAd({ adUnitId: AdUnit });//通用视频的广告位
            rewardedVideoAd.load().then(() => {
                rewardedVideoAd.show();
            }).catch(err => {
                cc.vv.EventMgr.dispatcher(cc.vv.EventConfig.hint, "今日视频次数用完了,明天再来吧");
                if(GStorage.getAdFail() < 1){//还没有失败
                    //可以点击了
                    GShare[GShare.shareType + "Clicked"] = false;
                    console.error("广告拉取失败，直接发放1次奖励!");
                    if(GShare.callBackSuccess){
                        //在本地记录失败
                        GStorage.setAdFail();
                        //次数加1
                        GStorage.shareAdTimesAdd1(GShare.shareType);
                        //发放奖励
                        GShare.callBackSuccess();
                    }
                }
            });
            //广告关闭后
            rewardedVideoAd.onClose(res => {
                //可以点击了
                GShare[GShare.shareType + "Clicked"] = false;
                rewardedVideoAd.offClose();
                if (res && res.isEnded || res === undefined) {
                    if(GShare.callBackSuccess){
                        console.log("看完视频，获得奖励！");
                        GStorage.shareAdTimesAdd1(GShare.shareType);
                        //文本提示
                        GShare.callBackSuccess();
                        GReport.scene(6, index);
                    }
                }
                else {
                    console.log("中途退出视频...");
                    cc.vv.EventMgr.dispatcher(cc.vv.EventConfig.hint,"只有观看完整视频才能获得奖励~");
                    if(GShare.failFunction) {
                        GShare.failFunction();
                    }
                }
            })
        }
    },

    /** 有奖分享或者视频广告的策略合集  
     * shareType：分享类型， 现有：tips  freeTips  ，必须    
     * callBackSuccess：分享或者视频广告成功后的回调（奖励）函数，必须       
     * failFunction：分享或者视频广告失败后的逻辑，可选  
     * 
     */
    shareToGetRewardAll(shareType, callBackSuccess, failFunction){
        //防止多重点击
        if(GShare[shareType + "Clicked"]){
            return;
        }else{
            GShare[shareType + "Clicked"] = true;
        }
        //记录对应的数据 特殊处理的失败函数
        GShare.shareType = shareType;
        GShare.callBackSuccess = callBackSuccess;
        GShare.failFunction = failFunction;
        //分享开关
        var switcher = cc.vv.DataManage.getShareStatus();
        var onlyVideo = CGame.getConfig().OnlyVideo;
        //判断是否在屏蔽分享区，并且那里的分享关闭
        var canShare = CGame.shieldAreaCanShare();
        //是否在白名单内部
        var isWhitelist = CSdk.isOpenidInWhitelist();
        //分享主逻辑
        if(cc.sys.platform === cc.sys.WECHAT_GAME){
            if(switcher === 1 && ((!onlyVideo && canShare) || isWhitelist)){//分享开光打开.可能分享
                if(GStorage.getAdFail() > 0){//没视频，只能分享
                    GShare.share();
                }else{
                    var shareAdInfo = CShareAdInfo.getShareAdInfo(shareType);
                    if(shareAdInfo.type <= 0){//随机策略
                        if(Math.random() <= shareAdInfo.shareRate){//随机分享
                            GShare.share();
                        }else{//随机视频
                            GShare.videoAd();
                        }
                    }else{//配置策略
                        var shareAdNum = CShareAdInfo.getShareAdNum(shareType);
                        if(shareAdNum < 1){//分享
                            GShare.share();
                        }else{//广告
                            GShare.videoAd();
                        }
                    }
                }
            }
            else{//分享开关关闭，只能看视频
                GShare.videoAd();
            }
        }
        else{//非微信平台，测试直接给奖励
            if(callBackSuccess){
                console.log("非微信平台，测试直接给奖励");
                callBackSuccess();
                GStorage.shareAdTimesAdd1(GShare.shareType);
                GShare[shareType + "Clicked"] = false;
            }
        }     
    },

    /**获取当前分享场景下一次是分享还是视频 true为视频  false为分享 */
    nextIsAd(shareType){
        var isAd = false;
        if(cc.sys.platform === cc.sys.WECHAT_GAME){
            var switcher = cc.vv.DataManage.getShareStatus();
            var shareAdInfo = CShareAdInfo.getShareAdInfo(shareType);
            var shareAdNum = CShareAdInfo.getShareAdNum(shareType);
            if(switcher === 1 && (GStorage.getAdFail() > 0 || shareAdNum < 1 || shareAdInfo.type <= 0)){//分享
                isAd = false;
            }else{
                isAd = true;
            }
        }
        return isAd;
    },

    /**获取当前时间，单位：毫秒 */
    getCurrentTime(){
        var date = new Date();
        var time = date.getTime();
        return time;
    },

    /**监听微信的显示（show）和隐藏（hide），同时也是分享回来的逻辑处理部分 */
    showAndHide(){
        if (cc.sys.platform === cc.sys.WECHAT_GAME){
            wx.onShow(function(){
                //获取场景值
                var index = GShare.getIndexByShareType(GShare.shareType) + 1;
                //可以点击了
                GShare[GShare.shareType + "Clicked"] = false; 
                if(GShare.isShare){
                    GShare.isShare = false;
                    if(GShare.cancel){
                        cc.vv.EventMgr.dispatcher(cc.vv.EventConfig.hint, "请不要发送给同一个朋友!");
                        GShare.cancel = false;
                        return;
                    }
                    //如果上次分享失败，这次超过时间给奖励，否则仍然失败
                    if(GShare["shareFail" + GShare.shareType]){
                        //获取当前时间
                        GShare.showTime = GShare.getCurrentTime();
                        var shareTime = (GShare.showTime - GShare.hideTime) / 1000;
                        if(shareTime >= CShareAdInfo.getConfig().shareFailLimitTime){
                            //成功
                            if(GShare.callBackSuccess){
                                GShare["shareFail" + GShare.shareType] = false;
                                //记录分享次数
                                GStorage.shareAdTimesAdd1(GShare.shareType);
                                //提示
                                //cc.vv.EventMgr.dispatcher(cc.vv.EventConfig.hint,"分享成功!");
                                GShare.callBackSuccess();
                                GReport.scene(4, index);
                            }
                        }else{
                            //提示
                            cc.vv.EventMgr.dispatcher(cc.vv.EventConfig.hint,"请不要发送给同一个朋友!");
                            if(GShare.failFunction) {
                                GShare.failFunction();
                            }
                        }
                    }else{
                        //获取当前时间
                        GShare.showTime = GShare.getCurrentTime();
                        var shareTime = (GShare.showTime - GShare.hideTime) / 1000;
                        //超过时间，并且随机数字大于给奖励概率
                        if(shareTime >= CShareAdInfo.getConfig().shareFailLimitTime && Math.random() < CShareAdInfo.getConfig().successRote){
                            if(GShare.callBackSuccess){
                                //记录分享次数
                                GStorage.shareAdTimesAdd1(GShare.shareType);
                                //成功奖励提示框？？
                                //cc.vv.EventMgr.dispatcher(cc.vv.EventConfig.hint,"分享成功!");
                                GShare.callBackSuccess();
                                GReport.scene(4, index);
                            }
                        }else{//记录当前分享场景失败，
                            GShare["shareFail" + GShare.shareType] = true;
                            //提示
                            cc.vv.EventMgr.dispatcher(cc.vv.EventConfig.hint,"请不要发送给同一个朋友!");
                            if (GShare.failFunction) {
                                GShare.failFunction();
                            }
                        }
                    }
                }else if(GShare.isNormalShare){
                    GShare.isNormalShare = false;
                    if(!GShare.cancel){
                        //cc.vv.EventMgr.dispatcher(cc.vv.EventConfig.hint,"分享成功!");
                    }else{
                        GShare.cancel = false;
                        //cc.vv.EventMgr.dispatcher(cc.vv.EventConfig.hint,"请不要发送给同一个朋友!");
                    }
                }
            });
            wx.onHide(function(){
                //记录分享开始时间，用于分享是否给奖励判断
                if(GShare.isShare){
                    //获取当前时间
                    GShare.hideTime = GShare.getCurrentTime();
                }
            });
        }
    },

    /**显示右上角的分享按钮 */
    showRightShareButton(){
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            wx.showShareMenu();
            wx.onShareAppMessage(()=> {
                return {
                    //分享文案和图
                    title: "中国太古禁忌神器，在这里拿到你手软！",
                    imageUrl: "https://fcs-1256906012.cos.ap-guangzhou.myqcloud.com/Line/png/1.png",
                    query: "&cover=" + 99 + "&scene=" + 99
                }
            });
        }
    },
};

window.GShare = GShare;
GShare.showAndHide();